package com.shanshui.webtest;

import android.annotation.SuppressLint;
import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import java.util.ArrayList;
import java.util.Arrays;

/**
 * @author mashanshui
 * @date 2022-05-07
 * @desc TODO
 */
public class MyWebView extends WebView implements View.OnKeyListener {
    private static final String TAG = "MyWebView";
    private ArrayList<String> hasUrl = new ArrayList<String>(
            Arrays.asList("id_card_authentication",
                    "emergency_contacts",
                    "personal_infomation",
                    "review_tips",
                    "add_bankcard",
                    "confirm_borrow",
                    "additionalInfomation"));
    private String mCurrentLoadUrl = "";

    public MyWebView(@NonNull Context context) {
        super(context);
        config();
    }

    public MyWebView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        config();
    }

    public MyWebView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        config();
    }

    @SuppressLint("JavascriptInterface")
    private void config() {
        WebSettings settings = getSettings();
        // 是否保存表单数据
        settings.setSaveFormData(true);
        // 是否当webview调用requestFocus时为页面的某个元素设置焦点， 默认值 true
        settings.setNeedInitialFocus(true);
        // 是否支持 viewport 属性，默认值 false// 页面通过`<meta name="viewport" ... />`自适应手机屏幕
        settings.setUseWideViewPort(true);
        // 是否使用 overviewmode 加载页面，默认值 false// 当页面宽 度大于 WebView 宽度时，缩小使页面宽度等于 WebView 宽度
        settings.setLoadWithOverviewMode(true);
        // 是否支持 Javascript，默认值 false
        settings.setJavaScriptEnabled(true);
        //设置缓存模式
        settings.setCacheMode(WebSettings.LOAD_DEFAULT);
        // 资源访问
        settings.setAllowContentAccess(true);
        // 资源加载
        settings.setLoadsImagesAutomatically(true);
        // 缩放(zoom)
        settings.setSupportZoom(true);
        // 是否支持缩放
        settings.setBuiltInZoomControls(false);
        settings.setDomStorageEnabled(true);
        setWebViewClient(new WebViewClient() {
            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                mCurrentLoadUrl = url;
            }
        });
        setWebChromeClient(new WebChromeClient(){
            @Override
            public void onReceivedTitle(WebView view, String title) {
                super.onReceivedTitle(view, title);
            }
        });
        setOnKeyListener(this);
    }

    @Override
    public boolean onKey(View view, int keyCode, KeyEvent keyEvent) {
        if (keyEvent.getAction() == KeyEvent.ACTION_DOWN) {
            //按返回键操作并且能回退网页
            if (keyCode == KeyEvent.KEYCODE_BACK) {
                if (canGoBack()) {
                    goBack();
                    return true;
                }
            }
        }
        return false;
    }
}
